import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class test {

    public static void main(String[] args) throws IOException, InterruptedException {
        String sql = "insert into scenery(sceneryUrl,sceneryName,countryUrl,countryName,info,`desc`)"
                + " values('#{sceneryUrl}','#{sceneryName}','#{countryUrl}','#{countryName}','#{info}','#{desc}')\n";
        String url = "http://scenery.nihaowang.com/";
        for (int i = 4018; i >=1; i--) {
            System.out.println(i + "页");
            Document document = Jsoup.connect("http://scenery.nihaowang.com/searchResult.html?s1=0&s2=0&s3=0")
                    .ignoreContentType(true).data("type", "Pre").data("pages", i + "").post();
            Elements all = document.getElementsByClass("sear_right_all");
            for (Element element : all) {
                String content = url;
                Elements searRightName = element.getElementsByClass("sear_right_name");
                for (Element name : searRightName) {
                    Thread.sleep(2000);
                    String sqltemp = sql;
                    Elements as = name.getElementsByTag("a");
                    Element a = as.get(0);
                    Element a1 = as.get(1);
                    String href = a.attr("href");
                    String scenery = a.text();
                    Document childDocument = Jsoup.connect(url + href)
                            .header("Cookie", "ASP.NET_SessionId=rkuv0dzelpw5zb0dutbrzw2e; zh_choose=s; Hm_lvt_ebbf3ce31194ceb57b6daa1fcb0d1b19=1679470583; Hm_lpvt_ebbf3ce31194ceb57b6daa1fcb0d1b19=1679477334")
                            .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36")
                            .header("Referer", "http://scenery.nihaowang.com/searchResult.html?s1=0&s2=0&s3=0")
                            .header("Host", "scenery.nihaowang.com")
                            .header("Connection", "keep-alive")
                            .cookie("Cookie", "ASP.NET_SessionId=rkuv0dzelpw5zb0dutbrzw2e; zh_choose=s; "
                                    + "Hm_lvt_ebbf3ce31194ceb57b6daa1fcb0d1b19=1679470583; Hm_lpvt_ebbf3ce31194ceb57b6daa1fcb0d1b19=1679477334")
                            .header("Cache-Control", "max-age=0")
                            .header("Upgrade-Insecure-Requests", "1")
                            .ignoreContentType(true).get();
                    System.out.println(childDocument.text());
                    Elements sceneryTopboxR = childDocument.getElementsByClass("scenery_topbox_r");
                    sceneryTopboxR = sceneryTopboxR.removeClass("scenery_renqi");
                    sceneryTopboxR = sceneryTopboxR.removeClass("scenery_dp_like");
                    String info = sceneryTopboxR.text();
                    String desc = childDocument.getElementsByClass("scenery_jianjie").text();
                    String href1 = a1.attr("href");
                    String country = a1.text();
                    // content += href + "\t" + scenery + "\t" + href1 + "\t" + country + "\t" + info + "\t" + desc + "\t";
                    sqltemp = sqltemp.replace("#{sceneryUrl}", url + href);
                    sqltemp = sqltemp.replace("#{sceneryName}", scenery);
                    sqltemp = sqltemp.replace("#{countryUrl}", href1);
                    sqltemp = sqltemp.replace("#{countryName}", country);
                    sqltemp = sqltemp.replace("#{info}", info);
                    sqltemp = sqltemp.replace("#{desc}", desc);
                    System.out.println(sqltemp);
                    new FileOutputStream("C:\\Users\\jianfengw\\Desktop\\scenery.txt", true).write(sqltemp.getBytes(StandardCharsets.UTF_8));
                }
                // System.out.println(content);
            }
        }

    }
}
